package com.org.zp.ctrl.job;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;

public class JobContentsActivity extends BaseActivity {
	private TextView title;
	private Button back_btn;
	private Button enable_btn;
	private TextView content_title;
	private TextView startdate;
	private TextView contents;
	private ProgressDialog progressDialog;
	private String id = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.job_contents);
		
		if (!(ActivityTaskManager.getInstance().containsName("JobContentsActivity"))) {
			ActivityTaskManager.getInstance().putActivity("JobContentsActivity",
					this);
		}
		
		title = (TextView) findViewById(R.id.headTitle);
		content_title = (TextView) findViewById(R.id.content_title);
		startdate = (TextView) findViewById(R.id.startdate);
		contents = (TextView) findViewById(R.id.news_contents);
		back_btn = (Button) findViewById(R.id.back);
		enable_btn = (Button) findViewById(R.id.top_right_button);
		enable_btn.setVisibility(View.INVISIBLE);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity("JobContentsActivity");
			}
		});
		title.setText("职场咨询");
		
		
		initdata();
	}

	private void initdata() {
		id = getIntent().getStringExtra("id");
		
		progressDialog = ProgressDialog.show(JobContentsActivity.this, "请等待...",
				"正在加载信息，请稍后...", true, true);
		new QueryContentAsyncTask().execute(URLHelper.mPrefix);
	}
	
	class QueryContentAsyncTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "articleDetail");
			map.put("article_id", id);
			
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				progressDialog.dismiss();
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				progressDialog.dismiss();
			} else {
				parseJson(result);
			}

		}
	}
	
	private void parseJson(String str) {
		progressDialog.dismiss();
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				JSONObject json = new JSONObject(str);
				
				content_title.setText(json.getString("title"));
				startdate.setText(json.getString("addtime"));
				contents.setText(Html.fromHtml(json.getString("content")));

			} else {
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity("JobContentsActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
